/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T009Medicine;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T008Dosage;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Spyros
 */
public class T009MedicineJpaController {

    public T009MedicineJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T009Medicine t009Medicine) {
        if (t009Medicine.getT008DosageCollection() == null) {
            t009Medicine.setT008DosageCollection(new ArrayList<T008Dosage>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<T008Dosage> attachedT008DosageCollection = new ArrayList<T008Dosage>();
            for (T008Dosage t008DosageCollectionT008DosageToAttach : t009Medicine.getT008DosageCollection()) {
                t008DosageCollectionT008DosageToAttach = em.getReference(t008DosageCollectionT008DosageToAttach.getClass(), t008DosageCollectionT008DosageToAttach.getId());
                attachedT008DosageCollection.add(t008DosageCollectionT008DosageToAttach);
            }
            t009Medicine.setT008DosageCollection(attachedT008DosageCollection);
            em.persist(t009Medicine);
            for (T008Dosage t008DosageCollectionT008Dosage : t009Medicine.getT008DosageCollection()) {
                T009Medicine oldT009MedicineOfT008DosageCollectionT008Dosage = t008DosageCollectionT008Dosage.getT009Medicine();
                t008DosageCollectionT008Dosage.setT009Medicine(t009Medicine);
                t008DosageCollectionT008Dosage = em.merge(t008DosageCollectionT008Dosage);
                if (oldT009MedicineOfT008DosageCollectionT008Dosage != null) {
                    oldT009MedicineOfT008DosageCollectionT008Dosage.getT008DosageCollection().remove(t008DosageCollectionT008Dosage);
                    oldT009MedicineOfT008DosageCollectionT008Dosage = em.merge(oldT009MedicineOfT008DosageCollectionT008Dosage);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T009Medicine t009Medicine) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T009Medicine persistentT009Medicine = em.find(T009Medicine.class, t009Medicine.getId());
            Collection<T008Dosage> t008DosageCollectionOld = persistentT009Medicine.getT008DosageCollection();
            Collection<T008Dosage> t008DosageCollectionNew = t009Medicine.getT008DosageCollection();
            List<String> illegalOrphanMessages = null;
            for (T008Dosage t008DosageCollectionOldT008Dosage : t008DosageCollectionOld) {
                if (!t008DosageCollectionNew.contains(t008DosageCollectionOldT008Dosage)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T008Dosage " + t008DosageCollectionOldT008Dosage + " since its t009Medicine field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T008Dosage> attachedT008DosageCollectionNew = new ArrayList<T008Dosage>();
            for (T008Dosage t008DosageCollectionNewT008DosageToAttach : t008DosageCollectionNew) {
                t008DosageCollectionNewT008DosageToAttach = em.getReference(t008DosageCollectionNewT008DosageToAttach.getClass(), t008DosageCollectionNewT008DosageToAttach.getId());
                attachedT008DosageCollectionNew.add(t008DosageCollectionNewT008DosageToAttach);
            }
            t008DosageCollectionNew = attachedT008DosageCollectionNew;
            t009Medicine.setT008DosageCollection(t008DosageCollectionNew);
            t009Medicine = em.merge(t009Medicine);
            for (T008Dosage t008DosageCollectionNewT008Dosage : t008DosageCollectionNew) {
                if (!t008DosageCollectionOld.contains(t008DosageCollectionNewT008Dosage)) {
                    T009Medicine oldT009MedicineOfT008DosageCollectionNewT008Dosage = t008DosageCollectionNewT008Dosage.getT009Medicine();
                    t008DosageCollectionNewT008Dosage.setT009Medicine(t009Medicine);
                    t008DosageCollectionNewT008Dosage = em.merge(t008DosageCollectionNewT008Dosage);
                    if (oldT009MedicineOfT008DosageCollectionNewT008Dosage != null && !oldT009MedicineOfT008DosageCollectionNewT008Dosage.equals(t009Medicine)) {
                        oldT009MedicineOfT008DosageCollectionNewT008Dosage.getT008DosageCollection().remove(t008DosageCollectionNewT008Dosage);
                        oldT009MedicineOfT008DosageCollectionNewT008Dosage = em.merge(oldT009MedicineOfT008DosageCollectionNewT008Dosage);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t009Medicine.getId();
                if (findT009Medicine(id) == null) {
                    throw new NonexistentEntityException("The t009Medicine with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T009Medicine t009Medicine;
            try {
                t009Medicine = em.getReference(T009Medicine.class, id);
                t009Medicine.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t009Medicine with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<T008Dosage> t008DosageCollectionOrphanCheck = t009Medicine.getT008DosageCollection();
            for (T008Dosage t008DosageCollectionOrphanCheckT008Dosage : t008DosageCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T009Medicine (" + t009Medicine + ") cannot be destroyed since the T008Dosage " + t008DosageCollectionOrphanCheckT008Dosage + " in its t008DosageCollection field has a non-nullable t009Medicine field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(t009Medicine);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T009Medicine> findT009MedicineEntities() {
        return findT009MedicineEntities(true, -1, -1);
    }

    public List<T009Medicine> findT009MedicineEntities(int maxResults, int firstResult) {
        return findT009MedicineEntities(false, maxResults, firstResult);
    }

    private List<T009Medicine> findT009MedicineEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T009Medicine.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T009Medicine findT009Medicine(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T009Medicine.class, id);
        } finally {
            em.close();
        }
    }

    public int getT009MedicineCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T009Medicine> rt = cq.from(T009Medicine.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
